﻿/*----------------------------------------------------------------
  //Copyright (C) 2012 赵国伟
  //文件功能描述：任务管理Ajax请求服务端处理程序。
  //创建人：赵国伟
  //生成时间：2012/04/18 16:15:00
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Shopping.Common;
using Shopping.Model;
using Shopping.BLL;

public partial class Manage_AS_TasksCommon : BaseManagePage
{
    private readonly TasksManage bllTask = new TasksManage();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string action = Request.QueryString["Action"] as string;
            switch (action)
            {
                case "SaveTask":  //保存任务； by 赵国伟 2012-04-18
                    SaveTask();
                    break;
                case "GetTaskInfo": //查询一条任务 by 赵国伟 2012-04-18
                    GetTaskInfo();
                    break;
                case "DeleteTask": //删除一条任务 by 赵国伟 2012-04-18
                    DeleteTask();
                    break;
                case "RunTask": //执行一条任务; by 赵国伟 2012-04-18
                    RunTask();
                    break;
                case "RunAllTask":  //执行全部的任务 by 赵国伟 2012-05-29
                    RunAllTask();
                    break;
            }
        }
        Response.End();
    }

    #region "保存任务； by 赵国伟 2012-04-18"
    /// <summary>
    /// 保存任务；
    /// </summary>
    private void SaveTask()
    {
        Response.Write(BLLHelper.Save<TasksManage, Tasks>("Save").ToJsonString());
    }
    #endregion

    #region "查询一条任务 by 赵国伟 2012-04-18"
    /// <summary>
    /// 查询一条任务
    /// </summary>
    private void GetTaskInfo()
    {
        int taskID = Utility.G<int>("TaskID");
        Tasks task = bllTask.GetTaskInfo(taskID);
        if (task != null)
            Response.Write(JSONHelper.ObjectToJson<Tasks>(task));
    }
    #endregion

    #region "删除一条任务 by 赵国伟 2012-04-18"
    /// <summary>
    /// 删除一条任务
    /// </summary>
    private void DeleteTask()
    {
        int taskID = Utility.G<int>("TaskID");
        Response.Write(bllTask.Delete(taskID).ToJsonString());
    }
    #endregion

    #region "执行一条任务； by 赵国伟 2012-04-18"
    /// <summary>
    /// 执行一条任务；
    /// </summary>
    private void RunTask()
    {
        int taskID = Utility.G<int>("TaskID");
        Response.Write(bllTask.RunTask(taskID).ToJsonString());
    }
    #endregion

    #region "执行全部的任务 by 赵国伟 2012-05-29"
    /// <summary>
    /// 执行全部的任务
    /// </summary>
    private void RunAllTask()
    {
        ReturnValue ret = new ReturnValue();
        List<TaskInfo> listOfTasks = bllTask.QueryAllTask();
        if (listOfTasks == null)
        {
            ret.Message = "系统未设置任何任务！";
            Response.Write(ret.ToJsonString());
            return;
        }
      
        foreach (TaskInfo task in listOfTasks)
        {
            ret = bllTask.RunTask(task);
            if (!ret.IsSucc)
            {
                ret.Message = "执行失败！（" + ret.Message + "）";
                break;
            }
        }
        Response.Write(ret.ToJsonString());
    }
    #endregion






}